package com.sskj.cndragon.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.presenter.SendRecordActivityPresenter;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.refresh.MVCSmartHelper;
import com.sskj.lib.util.APKVersionCodeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

public class SendRecordActivity extends BaseActivity<SendRecordActivityPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    SmartRefreshLayout coolRefreshView;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    private SlimAdapter<RecordListBean.RecordBean> slimAdapter;
    //    private MVCCoolHelper<List<RecordListBean.RecordBean>> mvcHelper;
    private MVCSmartHelper<List<RecordListBean.RecordBean>> mvcHelper;

    private String userID;


    @Override

    protected int getLayoutResId() {
        return R.layout.activity_send_record;
    }

    @Override
    public SendRecordActivityPresenter getPresenter() {
        return new SendRecordActivityPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        coolRefreshView.setRefreshHeader(new ClassicsHeader(this));
        userID = SPUtil.get(SPConfig.ID, "");
        toolbar.setLeftButtonOnClickListener(view -> finish());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setPaintColor(ContextCompat.getColor(this, R.color.divider_s))
                .setFirstDraw(false)
                .setLastDraw(false));
        slimAdapter = SlimAdapter.create().register(R.layout.item_send_record, new SlimInjector<RecordListBean.RecordBean>() {
            @Override
            public void onInject(RecordListBean.RecordBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tv_msg, userID.equals(data.getSelfStockUserId()) ?
                        App.INSTANCE.getString(R.string.zhuanzenggei) + data.getOtherUsername() :
                        data.getSelfUsername() + App.INSTANCE.getString(R.string.zhuanzeng))
                        .text(R.id.tv_money, userID.equals(data.getSelfStockUserId()) ?
                                "-" + NumberUtil.keep2(data.getCoinNum()) :
                                "+" + NumberUtil.keep2(data.getCoinNum()))
                        .text(R.id.tv_date, TimeFormatUtil.formatE(data.getCreateTime()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mvcHelper = new MVCSmartHelper<>(coolRefreshView);
        ModelRx2DataSource<RecordListBean.RecordBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<RecordListBean.RecordBean>)
                        page -> mPresenter.getSendRecord(page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.refresh();
    }

    @Override
    protected void initData() {
        super.initData();
        tvVersion.setText(App.INSTANCE.getString(R.string.bottom1) + APKVersionCodeUtils.getVerName(this));
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SendRecordActivity.class);
        context.startActivity(intent);
    }

}
